#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: workpiece_address_complement.py
# @time: 2021/4/12 14:55
# @desc:
import os

import pymongo

from .views.configs import *
from .utils.md5_tools import getFileMd5
from .utils.mongo_tools import total_number_generator_only_for_old_clamp, update_mongo_value
from .utils.oss.oss import upload_file_to_oss
from .utils.utils import read_json


myclient = pymongo.MongoClient(MONGO_URL)
mydb = myclient["do_center"]
history_clamp = mydb["history_clamp"]

cursor = history_clamp.find({})

history_clamp_list = []
for i in cursor:
    # 找出那些历史夹具，而不是之前我们做试验上传的数据
    if "temp_data" in i["file"][CATPART_OSS_ADDRESS]:
        continue
    print(i["name"])
    i["file"][WORKPIECE_CATPART_OSS_ADDRESS] = "/".join(i["file"][CATPART_OSS_ADDRESS].split("/")[:6]) + "/Work.CATPart"
    i["file"]["workpieceCatpartMD5"] = getFileMd5("data/" + "/".join(i["file"][WORKPIECE_CATPART_OSS_ADDRESS].split("/")[4:]))
    i["file"][WORKPIECE_STP_OSS_ADDRESS] = "/".join(i["file"][CATPART_OSS_ADDRESS].split("/")[:6]) + "/Work.stp"
    i["file"]["workpieceStpMD5"] = getFileMd5("data/" + "/".join(i["file"][WORKPIECE_STP_OSS_ADDRESS].split("/")[4:]))
    i["file"][WORKPIECE_STL_OSS_ADDRESS] = "/".join(i["file"][CATPART_OSS_ADDRESS].split("/")[:6]) + "/Work.stl"
    i["file"]["workpieceStlMD5"] = getFileMd5("data/" + "/".join(i["file"][WORKPIECE_STL_OSS_ADDRESS].split("/")[4:]))
    history_clamp_list.append(i)
    update_mongo_value(filter_condition={"number": i["number"]}, new_value={"$set": i}, database_name=MONGO_DATABASE_NAME, collection_name="history_clamp")


